package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.ApUserFollowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author lzq
 */
@Slf4j
@Service
@Transactional
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {


   /* @Resource
    private IWemediaClient iWemediaClient;*/

    @Autowired
    private CacheService cacheService;

    /**
     * 关注或取消
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userFollow(UserRelationDto dto) {
        try {
            /*//这里不去操作mysql，直接存到redis中
            if (dto.getOperation() == 0) {
                ApUserFollow apUserFollow = new ApUserFollow();
                //设置当前用户id
                apUserFollow.setUserId(ApUserThreadLocalUtil.get().getId());
                //关注者id
                apUserFollow.setFollowId(dto.getAuthorId());
                //关注者名字  注：这个name是从wmUsr表中获取
                WmUser wmUser = new WmUser();
                wmUser.setId(dto.getAuthorId());
                ResponseResult result = iWemediaClient.getWmUser(wmUser);
                if (result != null) {
                    String jsonStr = JSON.toJSONString(result.getData());
                    wmUser = JSON.parseObject(jsonStr, WmUser.class);
                    apUserFollow.setFollowName(wmUser.getName());
                }
                //时间
                apUserFollow.setCreatedTime(new Date());
                this.save(apUserFollow);
            } else {
                this.remove(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId, dto.getAuthorId()).
                        eq(ApUserFollow::getUserId, ApUserThreadLocalUtil.get().getId()));
            }
            return ResponseResult.okResult("操作成功");*/
            //存到redis中
            String key = "FOLLOW_BEHAVIOR_" /*+ ApUserThreadLocalUtil.get().getId().intValue()*/; //后段测试已通，需放开后联调
            String hashkey = String.valueOf(dto.getArticleId());
            if (dto.getOperation() == 0){
                //当前用户id+行为类型作为key、文章id作为hashkey,dto作为value
                String jsonString = JSON.toJSONString(dto);

                cacheService.hPutIfAbsent(key, hashkey, jsonString);

            } else if (dto.getOperation() == 1){
                //取消点赞

                cacheService.hDelete(key, hashkey);
            }
            return ResponseResult.okResult("操作成功");
        } catch (Exception e) {
            log.info("关注操作失败:{}", e);
            return ResponseResult.errorResult(500, "操作失败");
        }
    }
}
